<template>
  <div>
      <el-card  class="co-middle">
          <el-form :inline="true"  class="demo-form-inline">
             <el-form-item label="线路编号:" label-width="100px">
    <el-input v-model="pageing.number" placeholder="请输入线路编号" clearable></el-input>
  </el-form-item >
  <el-form-item label="线路名称:" label-width="160px">
      <el-input v-model="pageing.name" placeholder="请输入线路名称" clearable></el-input>
  </el-form-item >
   <el-form-item label="起始机构:"  label-width="160px">
    <template>
      <Treeselect
      style="margin-left:10px;"
       class="treeselect-main"
                v-model="pageing.startOrganId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择起始地机构"
                 :append-to-body="true"
              />
     </template>
  </el-form-item>
        </el-form>
        <!--  -->
        <el-form :inline="true"  class="demo-form-inline">
  <el-form-item label="目的地机构:" label-width="88px">
  <template>
      <Treeselect
      style="margin-left:10px;"
       class="treeselect-main"
                v-model="pageing.endOrganId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择目的地机构:"
                 :append-to-body="true"
              />
     </template>
  </el-form-item>
   <el-form-item class="anniu">
    <el-button type="danger" @click="search">搜索</el-button>
    <el-button @click="reset">重置</el-button>
  </el-form-item>

       </el-form>
      </el-card>
       <el-card>
        <el-row style="margin-bottom:20px;">
           <!-- <template #default="{row}"> -->
           <el-button  type="danger" @click="add">新增路线</el-button>
          <el-button  type="danger" @click="set">成本设置</el-button>
           <!-- </template> -->
        </el-row>
        <!-- tab表格 -->

  <el-table  :header-cell-style="{background:'#f8faff',color:'#818693'}"  :data="tableData" stripe style="width: 100%" >
    <!-- 左侧 -->
     <el-table-column type="expand">
      <template >
        <!-- {{row}} -->
      <el-table :data="tableData" style="width: 100%" :header-cell-style="{background:'#f8faff',color:'#818693'}">
          <el-table-column  prop="date"  label="日期" width="180"> </el-table-column>
          <el-table-column prop="date" label="姓名" width="180"> </el-table-column>
          <el-table-column prop="date" label="地址"> </el-table-column>
    </el-table>
      </template>
     </el-table-column>
    <el-table-column label="线路编号" width="120"  prop="number"> </el-table-column>
    <el-table-column prop="name" label="线路名称" width="155" > </el-table-column>
    <el-table-column prop="type" label="线路类型"  width="155" :formatter="type"> </el-table-column>
    <el-table-column prop="startOrganName" label="起始地机构" width="155"  > </el-table-column>
    <el-table-column prop="endOrganName" label="目的地机构" width="155" > </el-table-column>
    <el-table-column prop="distance" label="距离(千米)" width="155" > </el-table-column>
    <el-table-column prop="cost" label="成本(元)" width="155" > </el-table-column>
    <el-table-column prop="time" label="预计时间(分钟)" width="155" > </el-table-column>
    <el-table-column label="操作" width="200px" >
          <template >
            <div style="display: flex;align-items: center">
              <el-link style="color: #419eff" >修改 </el-link>
                 <el-divider direction="vertical" ></el-divider>
           <el-link style="color: red" >删除 </el-link>
           <el-divider direction="vertical" ></el-divider>
             <el-link style="color: #419eff" >增加车次 </el-link>
            </div>
          </template>
        </el-table-column>
  </el-table>
   <!-- 分页 -->
       <el-pagination
       class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageing.page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageing.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
      </el-card>
      <!-- 新增线路 -->
      <el-dialog
  title="新增线路"
  :visible.sync="showDialog"
  width="40%">
 <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="线路编号: " prop="name" >
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
    <el-form-item label="线路名称: " prop="name">
    <el-input v-model="ruleForm.name" ></el-input>
  </el-form-item>
  <el-form-item label="线路类型: " prop="region" clearable>
    <el-select v-model="ruleForm.region" placeholder="请选择活动区域" style="width:450px;">
      <el-option  v-for="item in freightmanagement.type" :key="item.id" :label="item.value" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
   <el-form-item label="起始地机构:"  label-width="90px" style="">
    <template>
      <Treeselect
      style="margin-left:10px;width:420px;"
       class="city"
                v-model="pageing.startOrganId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择起始地机构"

              />
     </template>
  </el-form-item>
  <el-form-item label="目的地机构:" label-width="90px">
  <template>
      <Treeselect
      style="margin-left:10px;width:420px;"
       class="city"
                v-model="pageing.endOrganId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择目的地机构:"

              />
     </template>
  </el-form-item>
 </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="showDialog = false" type="danger">确 定</el-button>
    <el-button  @click="showDialog = false">取 消</el-button>
  </div>
</el-dialog>
<!-- 成本设置 -->
<el-dialog
  title="成本设置"
  :visible.sync="dialogVisible"
  width="40%">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="线路类型"  label-width="150px" style="margin-right:50px;">
   <span>每公里平均成本（元）</span>
  </el-form-item>
  <el-form-item label="干线：" prop="name" label-width="150px" style="margin-right:50px;">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
   <el-form-item label="支线：" prop="name" label-width="150px"  style="margin-right:50px;">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
   <el-form-item label="接驳：" prop="name" label-width="150px"  style="margin-right:50px;">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
  </el-form>

 <div slot="footer" class="dialog-footer" >
    <el-button @click="dialogVisible = false" type="danger">确 定</el-button>
    <el-button  @click="dialogVisible = false">取 消</el-button>
  </div>

</el-dialog>
  </div>
</template>

<script>
import freightmanagement from '@/api/constant/freightmanagement'
import { geLinePageApi } from '@/api/line'
import Treeselect from '@riophae/vue-treeselect'
import { treeShapedApi } from '@/api/institution'
export default {
  components: { Treeselect },
  name: 'line-management',
  data () {
    return {
      dialogVisible: false,
      freightmanagement,
      ruleForm: {},
      showDialog: false,
      total: 0,
      form: {},
      tableData: [],
      tree: [],
      pageing: {
        endOrganId: null,
        startOrganId: null,
        page: 1,
        pageSize: 10,
        number: null,
        name: null
      },
      transportLineId: null,
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.initTree()
    this.getList()
  },
  methods: {
    set () {
      this.dialogVisible = true
    },
    add () {
      this.showDialog = true
    },
    handleSizeChange (newSize) {
      this.pageing.pageSize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.pageing.page = newPage
      this.getList()
    },
    async getList () {
      const { data: res } = await geLinePageApi(this.pageing)
      console.log(res)
      this.tableData = res.items
      this.total = +res.counts
    },
    search () {
      this.getList()
    },
    reset () {
      this.pageing = {
        number: null,
        name: null,
        endOrganId: null,
        startOrganId: null
      }
      this.getList()
    },
    normalizer (node) {
      // console.log(node)
      // 当子节点也就是children=[]时候去掉子节点
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children && node.children.length > 0 ? node.children : 0
      }
    },
    // 下拉树形结构
    async initTree () {
      const res = await treeShapedApi()
      this.tree = JSON.parse(res.data)
      // console.log(this.tree)
    },
    // 员工角色处理
    type: function (row) {
      // console.log(row)
      if (row.type === 1) {
        return '干线'
      } else if (row.type === 2) {
        return '支线'
      } else if (row.type === 3) {
        return '接驳线路'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .vue-treeselect__control {
    // padding-left: 5px;
    // padding-right: 5px;
    display: table;
    table-layout: fixed;
    /* width: 100%; */
    width: 220px;
    height: 36px;
    border: 1px solid #ddd;
    border-radius: 5px;
    background: #fff;
    transition-duration: 200ms;
    transition-property: border-color, box-shadow, width, height, background-color, opacity;
    transition-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
.anniu {
  margin-left: 50px;
}
.page {
  text-align: center;
}
::v-deep .el-select {
  width: 150px;
}
 .city {
  width: 420px;

}
.demo-ruleForm {
 border-radius:10px
}
::v-deep .dialog-footer {
  text-align: center;
}
</style>
